//
//  ViewController.swift
//  BARXSwiftTest
//
//  Created by 孙博岩 on 2018/5/25.
//  Copyright © 2018年 boai. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa


class ViewController: UIViewController, UITableViewDelegate {
    
    private var disposeBag = DisposeBag()
    private let cellID : String = "cell"

    let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String, Double>>(
        configureCell: { (_, tv, indexPath, element) in
            let cell = tv.dequeueReusableCell(withIdentifier: "cell")!
            cell.textLabel?.text = "\(element) @ row \(indexPath.row)"
            return cell
    },
        titleForHeaderInSection: { dataSource, sectionIndex in
            return dataSource[sectionIndex].model
    }
    )
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        setupUI()
    }
    
    func setupUI() -> Void {
        
        self.title = "RXSwitf Demo"
        
        let dataSource = self.dataSource
        
        let items = Observable.just([
            SectionModel(model: "First section", items: [
                1.0,
                2.0,
                3.0
                ]),
            SectionModel(model: "Second section", items: [
                1.0,
                2.0,
                3.0
                ]),
            SectionModel(model: "Third section", items: [
                1.0,
                2.0,
                3.0
                ])
            ])
        
        items
            .bind(to: tableView.rx.items(dataSource: dataSource))
            .disposed(by: disposeBag)
        
        tableView.rx
            .itemSelected
            .map { indexPath in
                return (indexPath, dataSource[indexPath])
            }
            .subscribe(onNext: { pair in
                let msg = "Tapped `\(pair.1)` @ \(pair.0)"
                print(msg)
            })
            .disposed(by: disposeBag)
        
        tableView.rx
            .setDelegate(self)
            .disposed(by: disposeBag)
    }
    
    override func viewDidLayoutSubviews() {
        tableView.frame = self.view.bounds
        
    }
    
    lazy var tableView:UITableView = {
        
        var tableView = UITableView()
        tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: cellID)
        tableView.tableFooterView = UIView()
        
//        let dataArray = Observable.just([
//            "1",
//            "2",
//            "3"
//            ])
//
//        //设置单元格数据（其实就是对 cellForRowAt 的封装）
//        dataArray
//            .bind(to: tableView.rx.items) { (tableView, row, element) in
//                let cell = tableView.dequeueReusableCell(withIdentifier: self.cellID)!
//                cell.textLabel?.text = "\(row)：\(element)"
//                return cell
//            }
//            .disposed(by: dispose)
//
//        tableView.rx.itemSelected.subscribe(onNext: { (indexPath) in
//            print("选中项的indexPath为：\(indexPath.row)")
//        }, onError: nil, onCompleted: nil, onDisposed: nil).disposed(by: dispose)
        
        self.view.addSubview(tableView)
        return tableView
    }()
    
//    lazy var dataArray:Observable<[String]> = {
//        var array:Observable<[String]> = Observable.E
//
//
//        return array
//    }()
}

